package com.gxkj.movie.helpers.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gxkj.movie.config.utils.OkHttpCli;
import com.gxkj.movie.helpers.ProShopHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sqyc
 */
@Component
@Slf4j
public class ProShopHelperImpl  implements ProShopHelper {

    @Value("${movie.server.host}")
    private String movieServerHost;

    @Autowired
    private OkHttpCli okHttpCli;

    @Override
    public JSONObject findProShopPageData(int pageNo, int pageSize, String fromSite) {
        String url = movieServerHost + "/proshop/dosearch.do";

        Map<String,String> params = new HashMap<>();
        params.put("pageNo",pageNo+"");
        params.put("pageSize",pageSize+"");
        params.put("fromSite",fromSite);

        String rep =  okHttpCli.doPost(url,params);
        if(StringUtils.isNotEmpty(rep)){

            JSONObject repJson = JSON.parseObject(rep);
            if(repJson.getInteger("status").equals(0)){
                return repJson.getJSONObject("data");
            }else{
                String msg = repJson.getString("message");
                log.warn("调用接口异常，url="+url+",返回数据为："+repJson.toJSONString());
                return null;
            }
        }else{
            return null;
        }
    }
}
